﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using Fodee.Controls;
using Fodee.DataEntity;
using Fodee.BusinessComponent;
using FodeeAdmin.SystemSettings;

namespace Fodee
{
    public partial class MainForm : BaseForm
    {
        private string tableID = string.Empty;
        private string tableName = string.Empty;
        private string tableNum = string.Empty;
        private bool isSelectOne = false;
        int refresh = 0;
        int tableIndex = 0;
        private Table currentTable = new Table();
        public bool IsLockEnter = false;

        TableBC tbc = new TableBC();
        private const string gpTableTxt = "桌台信息【{0}】";
        public MainForm()
        {
            InitializeComponent();
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void table1_Load(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ShowOpenTable();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormLoad();
        }

        private void timerDate_Tick(object sender, EventArgs e)
        {
            if (this.refresh == 15)
            {
                this.UpdateTableInfo();
                this.refresh = 0;
            }
            else
            {
                this.refresh++;
            }
            sbrDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public void BindingTables()
        {
            this.fpanleTable.Controls.Clear();
            FodeeDataSet dsTables = tbc.GetAll();
            int tempIndex = 0;
            foreach (FodeeDataSet.fod_TableRow dr in dsTables.fod_Table.Rows)
            {
                Table tbNew = this.NewTable(dr.ID,
                    dr.TableNum,
                    dr.TableName,
                    dr.TableStatusID,
                    "",
                    dr.TableStatusName,
                    dr.TableSize.ToString(),
                    "2012-12-12",
                    "郭靖",
                    "不要太辣",
                    tempIndex);

                tbNew.MouseDown += new MouseEventHandler(Table_MouseDown);
                tbNew.DoubleClick += new EventHandler(cItemOpenTable_Click);
                foreach (Control c in tbNew.Controls)
                {
                    c.MouseDown += new MouseEventHandler(Table_MouseDown);
                    c.DoubleClick += new EventHandler(cItemOpenTable_Click);
                }
                fpanleTable.Controls.Add(tbNew);
                tempIndex++;
            }
            this.Table_MouseDown(fpanleTable.Controls[0], new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            fpanleTable.Controls[0].Focus();
        }

        void tbNew_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Right:
                    this.tableIndex++;
                    if (tableIndex < fpanleTable.Controls.Count)
                        this.Table_MouseDown(fpanleTable.Controls[tableIndex], new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
                    else
                        this.tableIndex--;
                    break;
                case Keys.Left:
                    this.tableIndex--;
                    if (tableIndex >= 0)
                        this.Table_MouseDown(fpanleTable.Controls[tableIndex], new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
                    else
                        this.tableIndex++;
                    break;
                case Keys.Up:
                    this.tableIndex -= (this.fpanleTable.Width-40)/85;
                    if (tableIndex >= 0)
                        this.Table_MouseDown(fpanleTable.Controls[tableIndex], new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
                    else
                        this.tableIndex += (this.fpanleTable.Width - 40) / 85;
                    break;
                case Keys.Down:
                    this.tableIndex += (this.fpanleTable.Width - 40) / 85;
                    if (tableIndex < fpanleTable.Controls.Count)
                        this.Table_MouseDown(fpanleTable.Controls[tableIndex], new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
                    else
                        this.tableIndex -= (this.fpanleTable.Width - 40) / 85;
                    break;
                case Keys.Enter:
                    if (!IsLockEnter)
                    {
                        this.ShowOpenTable();
                    }
                    else
                    {
                        IsLockEnter = false;
                    }
                    break;
                default:
                    break;
            }
        }

        private Table NewTable(int id, string number, string name, int status,
            string custName, string statusName, string headCount, string openDate,
            string waiter, string remarks, int index)
        {
            Table tb = new Table();
            tb.TableID = id.ToString();
            tb.TableName = name;
            tb.TableNumber = number;
            tb.StatusID = status;
            tb.CustName = custName;
            tb.Status = statusName;
            tb.HeadCount = headCount;
            tb.OpenDate = openDate;
            tb.Waiter = waiter;
            tb.Remarks = remarks;
            tb.TableImage = (Image)rm.GetObject(this.GetImageByStatus(status));
            tb.ContextMenuStrip = this.cmTable;
            tb.Index = index;
            return tb;
        }

        private void cItemOpenTable_Click(object sender, EventArgs e)
        {
            ShowOpenTable();            
        }

        private void ShowOpenTable()
        {
            if (!this.isSelectOne)
            {
                MessageBox.Show("请选择一个桌台进行操作。");
                return;
            }
            switch (currentTable.StatusID)
            {
                case 1:
                    OpenTable otb = new OpenTable(this.tableID, this.tableName, this.tableNum);
                    otb.ShowDialog(this);
                    break;
                case 2:
                    OrderDetail ord = new OrderDetail(this.currentTable);
                    ord.ShowDialog();
                    break;
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ShowOpenTable();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ChangeTable ct = new ChangeTable();
            ct.ShowDialog();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            TogetherTable tt = new TogetherTable();
            tt.ShowDialog();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Bill bill = new Bill();
            bill.ShowDialog();
        }

        private void 点菜ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
        }

        private void cmTable_Opened(object sender, EventArgs e)
        {
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            InForm ifm = new InForm();
            ifm.Show();
        }

        private void FormLoad()
        {
            sbrDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            sbrSaleAmount.Text = string.Format(sbrSaleAmount.Text, "100.00");
            sbrUserName.Text = string.Format(sbrUserName.Text, "郭伟");
            sbrTableCount.Text = string.Format(sbrTableCount.Text, "23");
            this.gbTable.Text = string.Format(gpTableTxt, "未选桌台");
            BindingTables();
        }

        private string GetImageByStatus(int status)
        {
            string result = string.Empty;
            switch (status)
            {
                case 1:
                    result = "null";
                    break;
                case 2:
                    result = "button_group";
                    break;
                case 3:
                    result = "button_group";
                    break;
            }
            return result;
        }

        private void EnableMenuByStatus(int tableStatus)
        {
            switch (tableStatus)
            {
                case 1:
                    EnableAllMenu();
                    点菜ToolStripMenuItem.Enabled = false;
                    点菜ToolStripMenuItem1.Enabled = false;
                    tsbOrder.Enabled = false;
                    break;
                case 2:
                    EnableAllMenu();
                    开台ToolStripMenuItem.Enabled =false;
                    开台ToolStripMenuItem1.Enabled = false;
                    tsbOpen.Enabled = false;
                    break;
                default:
                    break;

            }
        }

        private void EnableAllMenu()
        {
            foreach (ToolStripItem t in toolStrip1.Items)
            {
                t.Enabled = true;
            }
            foreach (ToolStripItem c in cmTable.Items)
            {
                c.Enabled = true;
            }
            foreach (ToolStripMenuItem m in ((ToolStripMenuItem)menuStrip1.Items[1]).DropDownItems)
            {
                m.Enabled = true;
            }
        }

        public void UpdateTableInfo()
        {
            foreach (Table t in fpanleTable.Controls)
            {
                FodeeDataSet.fod_TableRow drTable 
                    = tbc.GetById(int.Parse(t.TableID)).fod_Table.Rows[0] as FodeeDataSet.fod_TableRow;
                t.StatusID 
                    = drTable.TableStatusID;
                t.Status 
                    = drTable.TableStatusName;
                t.TableImage 
                    = (Image)rm.GetObject(this.GetImageByStatus(drTable.TableStatusID));
                this.EnableMenuByStatus(t.StatusID);

                if (t.Index == this.tableIndex)
                {
                    this.Table_MouseDown(t, new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
                }
            }
        }

        private void Table_MouseDown(object sender, MouseEventArgs e)
        {
            string id = string.Empty;
            Table tb = new Table();
            switch (sender.GetType().Name)
            {
                case "Table":
                    id = ((Table)sender).TableID;
                    tb = (Table)sender;
                    break;
                case "Label":
                    id = ((Label)sender).Parent.Controls["labTableID"].Text;
                    tb = (Table)((Label)sender).Parent;
                    break;
                case "PictureBox":
                    id = ((PictureBox)sender).Parent.Controls["labTableID"].Text;
                    tb = (Table)((PictureBox)sender).Parent;
                    break;
            }

            if (e.Button == MouseButtons.Left
                || e.Button == MouseButtons.Right)
            {                
                this.labCustName.Text = tb.CustName;
                this.labStatus.Text = tb.Status;
                this.labHeadCount.Text = tb.HeadCount;
                this.labOpenDate.Text = tb.OpenDate;
                this.labWaiter.Text = tb.Waiter;
                this.labRemarks.Text = tb.Remarks;
                this.gbTable.Text = string.Format(gpTableTxt, tb.TableName);
                this.isSelectOne = true;
                this.tableID = tb.TableID;
                this.tableName = tb.TableName;
                this.tableNum = tb.TableNumber;
                this.tableIndex = tb.Index;
                tb.BorderStyle = BorderStyle.Fixed3D;
                this.EnableMenuByStatus(tb.StatusID);

                foreach (Control c in this.fpanleTable.Controls)
                {
                    if (((Table)c).TableID == id)
                    {
                        ((Table)c).IsMarked = true;
                        ((Table)c).BorderStyle = BorderStyle.Fixed3D;
                    }
                    else
                    {
                        ((Table)c).IsMarked = false;
                        ((Table)c).BorderStyle = BorderStyle.None;
                    }
                }
                this.currentTable = tb;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (MessageBox.Show("确定要关闭系统？","关闭", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    Application.Exit();
                }
                else
                    e.Cancel = true;
            }
        }
        protected override void OnResize(EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            base.OnResize(e);
        }

        private void LockTSMI_Click(object sender, EventArgs e)
        {
            Lock lk = new Lock();
            lk.ShowDialog(this);
        }
    }
}